//
//  SampuxEquipmentListView.m
//  Sampux
//
//  Created by rzm on 16/7/7.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxEquipmentListView.h"
#import "SampuxListTableViewCell.h"
#import "SampuxDeleteEquitmentView.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefreshStateHeader.h"

@interface SampuxEquipmentListView ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL editState;
}
@property (nonatomic, strong) SampuxDeleteEquitmentView *deleteEquipmentView;
@property (assign, nonatomic) BOOL swipeGestureStarted;
@property(nonatomic,assign) BOOL isOffLineOrNot;
@property (nonatomic, strong) SampuxListModel *model;

@end

@implementation SampuxEquipmentListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _dataSourceArr = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}

- (SampuxDeleteEquitmentView *)deleteEquipmentView
{
    if (_deleteEquipmentView == nil) {
        _deleteEquipmentView = [[SampuxDeleteEquitmentView alloc]initWithFrame:self.frame];
        __weak typeof(SampuxEquipmentListView *) wSelf = self;
        self.deleteEquipmentView.cancelBlock = ^(){
            [wSelf cancelDeleteEquipMent];
        };
    }
    return _deleteEquipmentView;
}

- (void)setupSubView
{
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 64 + 10, kScreenWidth - 20, kScreenHeight - 10 - 64 - 49)];
    [self addSubview:_listTableView];
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _listTableView.dataSource = self;
    _listTableView.delegate = self;
    _listTableView.backgroundColor = THSystemClearColor;
    
    MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    _listTableView.mj_header = header;
    [_listTableView.mj_header beginRefreshing];
    
}

- (void)refreshData
{
    [GBInterMaster_CommForSystem requestDeviceList:0];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
    cell.frame = _listTableView.bounds;
    if (cell == nil) {
        cell = [[SampuxListTableViewCell alloc]initWithFrame:_listTableView.bounds];
    }
    SampuxListModel *model = self.dataSourceArr[indexPath.row];
    cell.listModel = model;
    cell.redView.backgroundColor = THSystemClearColor;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,  Sampux6HScale(65), _listTableView.width, 1)];
    [cell addSubview:view];    
    view.backgroundColor = THBlackAlplaColor(0.2);
    cell.backgroundColor = THWhiteAlplaColor(0.1);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Sampux6HScale(65);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.redView.backgroundColor = THSystemRedColor;
    cell.backgroundColor = THWhiteAlplaColor(0.4);
    if (cell.listModel.isOnLine == YES) {
        if (self.listDelegate && [self.listDelegate respondsToSelector:@selector(listTableViewSeletedRowIsONLine:andDeviceModel:)]) {
            [self.listDelegate listTableViewSeletedRowIsONLine:indexPath andDeviceModel:cell.listModel];
        }
    } else {
        if (self.listDelegate &&[self.listDelegate respondsToSelector:@selector(listTableViewSeletedRowIsOffLine:andDeviceModel:)]) {
            [self.listDelegate listTableViewSeletedRowIsOffLine:indexPath andDeviceModel:cell.listModel];
        }
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
    cell.redView.backgroundColor = THSystemClearColor;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    self.swipeGestureStarted = YES;
    SampuxListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.arrowBtn.hidden = YES;
    NSLog(@"begin");
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.swipeGestureStarted) {
        NSLog(@"didEndEditingRowAtIndexPath");
        self.swipeGestureStarted = NO;
        SampuxListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.arrowBtn.hidden = NO;
    }
}

//删除cell方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteEquipmentViewshow];
    SampuxListModel *model = self.dataSourceArr[indexPath.row];
    self.deleteEquipmentView.model = model;

    if (model.isPossessor == YES) {
        self.deleteEquipmentView.warnLab.text = @"设备删除后您将不再拥有此设备的所有权!";
    }else
    {
        self.deleteEquipmentView.warnLab.text = @"设备删除后您将不再拥有此设备的使用权!";
    }
    __weak typeof(SampuxEquipmentListView *) wSelf = self;
      self.deleteEquipmentView.deleteBlock = ^(){
          
          if (editingStyle == UITableViewCellEditingStyleDelete) {
          dispatch_async(dispatch_get_main_queue(), ^{
              NSUInteger row = [indexPath row]; //获取当前行
              [wSelf.dataSourceArr removeObjectAtIndex:row]; //在数据中删除当前对象
              [wSelf.listTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//数组执行删除操作
          });
        }
    };
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)setDataSourceArr:(NSMutableArray *)dataSourceArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _dataSourceArr = dataSourceArr;
        [_listTableView reloadData];
        [_listTableView.mj_header endRefreshing];
    });
}

- (void)cancelDeleteEquipMent
{
    [self.deleteEquipmentView removeFromSuperview];
}

- (void)deleteEquipmentViewshow
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.deleteEquipmentView];
}

@end
